package eiisan.datatime;

import java.time.*;
import java.time.temporal.*;
import java.util.Date;
import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;

import static java.util.Collections.addAll;

/**
 * @author eii
 * @sice 2020/11/9
 */
public class Java8TimeTest {
    /**
     * 获取周第一天
     *
     * @param date
     * @return
     */
    public static Date getStartDayOfWeek(TemporalAccessor date) {
        TemporalField fieldISO = WeekFields.of(Locale.CHINA).dayOfWeek();
        LocalDate localDate = LocalDate.from(date);
        LocalDate with = localDate.with(ChronoField.DAY_OF_WEEK, 1);
        return Date.from(with.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 获取周最后一天
     *
     * @param date
     * @return
     */
    public static Date getEndDayOfWeek(TemporalAccessor date) {
        TemporalField fieldISO = WeekFields.of(Locale.CHINA).dayOfWeek();
        LocalDate localDate = LocalDate.from(date);
        LocalDate with = localDate.with(ChronoField.DAY_OF_WEEK, 7);
        return Date.from(with.atStartOfDay(ZoneId.systemDefault()).plusDays(1L).minusNanos(1l).toInstant());
    }


    public static void main(String[] args) {
        LocalDate localDate = LocalDate.now();
        Date startDayOfWeek = getStartDayOfWeek(localDate);
        Date endDayOfWeek = getEndDayOfWeek(localDate);
        System.out.println(startDayOfWeek);
        System.out.println(endDayOfWeek);

//2020-09-03
        System.out.println(localDate);
//2020, 9, 3
        System.out.println(localDate.getYear() + ", " + localDate.getMonthValue() + ", " + localDate.getDayOfMonth());

        LocalDate localDate2 = LocalDate.of(2017, 3, 3);
//2017-03-03
        System.out.println(localDate2);

        System.out.println("-------");

        LocalDate localDate3 = LocalDate.of(2010, 3, 25);
        MonthDay monthDay = MonthDay.of(localDate3.getMonth(), localDate3.getDayOfMonth());
        MonthDay monthDay2 = MonthDay.from(LocalDate.of(2011, 3, 26));
//not equals
        if (monthDay.equals(monthDay2)) {
            System.out.println("equals");
        } else {
            System.out.println("not equals");
        }

        LocalTime time = LocalTime.now();
//09:25:24.942
        System.out.println(time);

        LocalTime time2 = time.plusHours(3).plusMinutes(20);
//12:45:24.942
        System.out.println(time2);

        LocalDate localDate1 = localDate.plus(2, ChronoUnit.WEEKS);
//2020-09-17
        System.out.println(localDate1);

        LocalDate localDate4 = localDate.minus(2, ChronoUnit.MONTHS);
//2020-07-03
        System.out.println(localDate4);

        System.out.println("-------");

        Clock clock = Clock.systemDefaultZone();
//1599096324942
        System.out.println(clock.millis());

        System.out.println("-------");

        LocalDate localDate5 = LocalDate.now();
        LocalDate localDate6 = LocalDate.of(2017, 3, 18);
//LocalDate isAfter isBefore
        System.out.println(localDate5.isAfter(localDate6));
        System.out.println(localDate5.isBefore(localDate6));
        System.out.println(localDate5.equals(localDate6));

        System.out.println("-------");

        Set<String> set = ZoneId.getAvailableZoneIds();
        Set<String> treeSet = new TreeSet<String>() {
            {
                addAll(set);
            }
        };
//Africa/Abidjan
//Africa/Accra
//Africa/Addis_Ababa
        treeSet.stream().limit(3).forEach(System.out::println);
        System.out.println("-------");

        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        LocalDateTime localDateTime = LocalDateTime.now();
//2020-09-03T09:25:24.959
        System.out.println(localDateTime);

        ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId);
//ZonedDateTime.of
//2020-09-03T09:25:24.959+08:00[Asia/Shanghai]
        System.out.println(zonedDateTime);

        System.out.println("-------");

        YearMonth yearMonth = YearMonth.now();
//2020-09
        System.out.println(yearMonth);
//30
        System.out.println(yearMonth.lengthOfMonth());
//true isLeapYear
        System.out.println(yearMonth.isLeapYear());

        System.out.println("-------");
//YearMonth.of 2016 2
        YearMonth yearMonth1 = YearMonth.of(2016, 2);
//2016-02
        System.out.println(yearMonth1);
//29
        System.out.println(yearMonth1.lengthOfMonth());
//366
        System.out.println(yearMonth1.lengthOfYear());
//true
        System.out.println(yearMonth1.isLeapYear());

        System.out.println("-------");
// LocalDate
        LocalDate localDate7 = LocalDate.now();
//2020-09-03
        System.out.println("localDate7 = " + localDate7);
        LocalDate localDate8 = LocalDate.of(2018, 8, 20);
//Period 和 Duration。两个类看表示时间量或两个日期之间的差，两者之间的差异为：Period基于日期值，而Duration基于时间值
//see: https://blog.csdn.net/neweastsun/article/details/88770592
//Period 期间
        Period period = Period.between(localDate7, localDate8);
//-2
        System.out.println(period.getYears());
//0
        System.out.println(period.getMonths());
//-14
        System.out.println(period.getDays());
//period.isNegative 可以用于判断endDate是否大于startDate：
        System.out.println(period.isNegative());
        System.out.println("-------");
//2020-09-03T02:15:16.909Z
        System.out.println(Instant.now());
//Duration
        LocalTime start = LocalTime.of(2, 20, 27, 1024);
        LocalTime end = LocalTime.of(3, 22, 27, 1544);

        long seconds = Duration.between(start, end).getSeconds();
//3720   计算秒级 差
        System.out.println(seconds);

        LocalTime start2 = LocalTime.of(1, 22, 27, 1024);
        LocalTime end2 = LocalTime.of(3, 22, 27, 1544);
//520  只计算纳秒位的 差
        System.out.println(Duration.between(start2, end2).getNano());
    }
}
